package com.ricervcvcer.goods.controller;

import com.ricervcvcer.common.util.ResultEntity;
import com.ricervcvcer.goods.entity.TbSku;
import com.ricervcvcer.goods.entity.TbStock;
import com.ricervcvcer.goods.service.ITbSkuService;
import com.ricervcvcer.goods.service.ITbSpuService;
import com.ricervcvcer.goods.service.ITbStockService;
import com.ricervcvcer.goods.vo.SkuParamVO;
import com.ricervcvcer.goods.vo.SpecSpuSkuVO;
import com.ricervcvcer.goods.vo.SpuParamVO;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("goods")
public class GoodsController {
    @Autowired
    private ITbSkuService skuService;

    @Autowired
    private ITbSpuService spuService;

    @Autowired
    private ITbStockService stockService;

    @GetMapping("id/{id}")
    public TbSku getSkuById(@PathVariable("id")Long id){
        return skuService.getById(id);
    }

    @ApiOperation("根据SPU 查询sPu详细信息")
    @GetMapping("joinById/{id}")
    public ResultEntity joinById(@PathVariable("id")Long id){
        return ResultEntity.successWithData( spuService.selectJoin(id));
    }

    @GetMapping("getStockById/{id}")
    public TbStock getStockById(@PathVariable("id")Long id){
        return stockService.getById(id);
    }

    @GetMapping("getSkuBySpuId/{spu_id}")
    @ApiParam("根据SpuId 查询对应的sku")
    public ResultEntity getSkuBySpuId(
            @ApiParam("SPU_ID") @PathVariable("spu_id") Long spu_id){
         List<SkuParamVO> skuParamVOS = spuService.getSkuBySpuId(spu_id);
        return ResultEntity.successWithData(skuParamVOS);
    }

    @Transactional
    @ApiOperation("添加一个商品信息")
    @RequestMapping("add")
    public ResultEntity create(@ApiParam("接收商品封装对象") @RequestBody SpecSpuSkuVO obj){
        spuService.create(obj);
        return ResultEntity.successWithoutData();
    }

    @ApiOperation("修改商品spu")
    @PostMapping("editSpu")
    public ResultEntity editSpu(@RequestBody SpuParamVO sp){
        spuService.editSpu(sp);
        return ResultEntity.successWithoutData();
    }
}